// 设计相关接口
package controllers

// 控制层所有公开的接口函数必须验证用户身份

import (
	"net/http"
	"strconv"

	"bsm/logger"
	"bsm/models"
	"bsm/services"

	"github.com/gin-gonic/gin"
)

type DesignController interface {
	GetOrgList(ctx *gin.Context) // 获取组织列表

	GetProjectList(ctx *gin.Context) // 获取项目列表

	CreateProjectBudget(ctx *gin.Context)
	CreateProjectBudgetFrom(ctx *gin.Context) // 从已有项目创建新项目

	GetNormLabor(ctx *gin.Context)       //获取标准人工项（标准人工定额）
	GetNormMaterial(ctx *gin.Context)    //获取标准材料项（甲供材料库）
	GetProjectReviewer(ctx *gin.Context) // 获取项目审核人

	GetProjectBase(ctx *gin.Context)    // 获取项目立项信息
	UpdateProjectBase(ctx *gin.Context) // 更新项目立项信息

	GetProjectResume(ctx *gin.Context)    // 获取项目摘要信息
	LoadProjectLabor(ctx *gin.Context)    // 加载项目人工条目信息
	LoadProjectMaterial(ctx *gin.Context) // 加载项目材料条目信息
	LoadProjectOtherFee(ctx *gin.Context) // 加载项目其他费用条目信息
	GetMaterialPrice(ctx *gin.Context)    // 获取材料单价
	GetNormConversion(ctx *gin.Context)   // 获取单位转换系数
	GetConstructionCost(ctx *gin.Context) // 获取项目施工成本

	SaveProjectLabor(ctx *gin.Context)
	SaveProjectMaterial(ctx *gin.Context)
	SaveProjectOtherFee(ctx *gin.Context)
	SaveProject(ctx *gin.Context)

	SetProjectFiled(ctx *gin.Context) // 项目存档

	DeleteProject(ctx *gin.Context) // 删除项目

	GetProjectBalance(ctx *gin.Context)  // 获取项目库存信息
	GetProjectOutbound(ctx *gin.Context) // 获取项目出库信息
}

type designControllerImpl struct {
	svcDesign services.DesignService
	svcImport services.ImportService
	svcExport services.ExportService
	svcAuth   services.AuthService
}

func NewDesignController(designSvc services.DesignService, importSvc services.ImportService, exportSvc services.ExportService, authSvc services.AuthService) DesignController {
	return &designControllerImpl{
		svcDesign: designSvc,
		svcImport: importSvc,
		svcExport: exportSvc,
		svcAuth:   authSvc,
	}
}

func (c *designControllerImpl) GetOrgList(ctx *gin.Context) {

	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {
		return
	}

	orgList, err := c.svcDesign.GetOrgList()
	if err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":     "controllers",
			"operation": "GetOrgList",
		}).Errorf("获取组织列表失败: %v", err)
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "获取组织列表失败",
		})
		return
	}
	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code:  0,
		Msg:   "success",
		Count: len(orgList), // 总记录数
		Data:  orgList,
	})
}

// GetNormLabor 获取人工定额规范
func (c *designControllerImpl) GetNormLabor(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {
		return
	}

	ncode := ctx.Query("norm_code")
	data, err := c.svcDesign.GetDesignLabor(ncode)
	if err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":     "controllers",
			"operation": "GetNormLabor",
			"norm_code": ncode,
		}).Errorf("获取人工定额规范失败: %v", err)
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "获取人工定额规范失败",
		})
		return
	}
	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code:  0,
		Msg:   "success",
		Count: len(data), // 总记录数
		Data:  data,
	})
}

// GetNormMaterial 获取材料定额规范
func (c *designControllerImpl) GetNormMaterial(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {

		return
	}

	mcode := ctx.Query("mtl_code")
	data, err := c.svcDesign.GetDesignMaterial(mcode)
	if err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":     "controllers",
			"operation": "GetNormMaterial",
			"mtl_code":  mcode,
		}).Errorf("获取材料定额规范失败: %v", err)
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "获取材料定额规范失败",
		})
		return
	}
	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code:  0,
		Msg:   "success",
		Count: len(data), // 总记录数
		Data:  data,
	})
}

// GetProjectReviewer 获取项目审核人
func (c *designControllerImpl) GetProjectReviewer(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {

		return
	}

	orgcode := ctx.MustGet("org_code").(string)
	data, err := c.svcDesign.GetProjectReviewer(orgcode)
	if err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":     "controllers",
			"operation": "GetProjectReviewer",
			"org_code":  orgcode,
		}).Errorf("获取项目审核人失败: %v", err)
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "获取项目审核人失败",
		})
		return
	}
	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code:  0,
		Msg:   "success",
		Count: len(data), // 总记录数
		Data:  data,
	})
}

// CreateProjectBudget 创建预算项目
func (c *designControllerImpl) CreateProjectBudget(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {
		return
	}

	var req models.ProjectBase
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "预算项目信息格式错误",
		})
		return
	}

	// 调用服务层的创建方法
	code, err := c.svcDesign.CreateProject(&req)
	if err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":     "controllers",
			"operation": "CreateProjectBudget",
			"org_code":  req.OrgCode,
		}).Errorf("创建预算项目失败: %v", err)
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "创建预算项目失败",
		})
		return
	}
	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code: 0,
		Msg:  "success",
		Data: code,
	})
}

// CreateProjectBudgetFrom 从项目创建项目
func (c *designControllerImpl) CreateProjectBudgetFrom(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {
		return
	}
	var req struct {
		DesignCode string `json:"design_code" binding:"required"`
		UserId     string `json:"user_id" binding:"required"`
	}
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目信息格式错误",
		})
		return
	}
	designCode := req.DesignCode

	designer := req.UserId
	// orgcode := ctx.MustGet("org_code").(string)
	if designCode == "" {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "设计项目编号不能为空",
		})
		return
	}
	// 调用服务层的创建方法
	code, err := c.svcDesign.CreateProjectFrom(designCode, designer)
	if err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":       "controllers",
			"operation":   "CreateProjectBudgetFrom",
			"design_code": designCode,
		}).Errorf("从项目创建预算项目失败: %v", err)
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "从项目创建预算项目失败",
		})
		return
	}
	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code: 0,
		Msg:  "success",
		Data: code,
	})
}

// GetProjectBase 获取项目立项信息
func (c *designControllerImpl) GetProjectBase(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {

		return
	}

	designCode := ctx.Query("design_code")
	if designCode == "" {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "设计项目编号不能为空",
		})
		return
	}
	data, err := c.svcDesign.GetProjectBase(designCode)
	if err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":       "controllers",
			"operation":   "GetProjectBase",
			"design_code": designCode,
		}).Errorf("获取项目立项信息失败: %v", err)
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "获取项目立项信息失败",
		})
		return
	}
	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code: 0,
		Msg:  "success",
		Data: data,
	})
}

// UpdateProjectBase 更新项目立项信息
func (c *designControllerImpl) UpdateProjectBase(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {

		return
	}

	var req models.ProjectBase
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目立项信息格式错误",
		})
		return
	}

	if err := c.svcDesign.UpdateProjectBase(&req); err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":       "controllers",
			"operation":   "UpdateProjectBase",
			"design_code": req.DesignCode,
		}).Errorf("更新项目立项信息失败: %v", err)
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "更新项目立项信息失败",
		})
		return
	}
	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code: 0,
		Msg:  "success",
	})
}

// SaveProjectLabor 保存项目设计人工数据
func (c *designControllerImpl) SaveProjectLabor(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {
		return
	}

	type laborstruct struct {
		DesignCode string             `json:"design_code"`
		DesignType int                `json:"design_type"`
		LaborTable []models.LaborItem `json:"labor_table"`
	}

	var req laborstruct
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计人工数据格式错误",
		})
		return
	}
	if err := c.svcDesign.SaveProjectLabor(req.DesignCode, req.DesignType, req.LaborTable); err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":       "controllers",
			"operation":   "SaveProjectLabor",
			"design_code": req.DesignCode,
		}).Errorf("保存项目设计人工数据失败: %v", err)
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "保存项目设计人工数据失败",
		})
		return
	}
	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code: 0,
		Msg:  "success",
	})
}

// SaveProjectMaterial 保存项目设计材料数据
func (c *designControllerImpl) SaveProjectMaterial(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {
		return
	}

	type materialstruct struct {
		DesignCode    string                `json:"design_code"`
		DesignType    int                   `json:"design_type"`
		MaterialTable []models.MaterialItem `json:"material_table"`
		RefreshPrice  bool                  `json:"refresh_price"` //是否刷新价格
	}
	var req materialstruct
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计材料数据格式错误",
		})
		return
	}
	if err := c.svcDesign.SaveProjectMaterial(req.DesignCode, req.DesignType, req.MaterialTable, req.RefreshPrice); err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":       "controllers",
			"operation":   "SaveProjectMaterial",
			"design_code": req.DesignCode,
		}).Errorf("保存项目设计材料数据失败: %v", err)
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "保存项目设计材料数据失败",
		})
		return
	}
	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code: 0,
		Msg:  "success",
	})
}

// SaveProject 保存项目设计表格数据
func (c *designControllerImpl) SaveProject(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {

		return
	}

	var req models.ProjectTable
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	if err := c.svcDesign.SaveProjectTable(req); err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":       "controllers",
			"operation":   "SaveProject",
			"design_code": req.DesignCode,
		}).Errorf("保存项目设计表格数据失败: %v", err)
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "保存项目设计表格数据失败",
		})
		return
	}
	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code: 0,
		Msg:  "success",
	})
}

// 获取项目概述
func (c *designControllerImpl) GetProjectResume(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {

		return
	}

	designcode := ctx.Query("design_code")
	if designcode == "" {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计编号不能为空",
		})
		return
	}
	dtype := ctx.Query("design_type")
	design_type, err := strconv.Atoi(dtype)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计类型错误",
		})
		return
	}

	if design_type != models.DesignType_Budget && design_type != models.DesignType_Settlement && design_type != models.DesignType_Audit {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计类型错误",
		})
		return
	}

	data, err := c.svcDesign.GetProjectResume(designcode, design_type)
	if err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":       "controllers",
			"operation":   "GetProjectResume",
			"design_code": designcode,
		}).Errorf("获取项目设计表格数据失败: %v", err)
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "获取项目设计表格数据失败",
		})
		return
	}

	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code: 0,
		Msg:  "success",
		Data: data,
	})
}

// 获取项目列表
func (c *designControllerImpl) GetProjectList(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {
		return
	}

	// 专门检查 design_type
	design_type, _ := strconv.Atoi(ctx.Query("design_type"))

	orgcode := ctx.MustGet("org_code").(string)

	data, err := c.svcDesign.GetProjectList(orgcode, design_type)
	if err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":       "controllers",
			"operation":   "GetProjectList",
			"org_code":    orgcode,
			"design_type": design_type,
		}).Errorf("获取项目列表失败: %v", err)
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "获取项目列表失败",
		})
		return
	}
	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code:  0,
		Msg:   "success",
		Count: len(data), // 总记录数
		Data:  data,
	})
}

// LoadProjectLabor 加载项目人工条目信息
func (c *designControllerImpl) LoadProjectLabor(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {

		return
	}

	designcode := ctx.Query("design_code")
	if designcode == "" {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计编号不能为空",
		})
		return
	}
	dtype := ctx.Query("design_type")
	design_type, err := strconv.Atoi(dtype)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计类型错误",
		})
		return
	}

	if design_type != models.DesignType_Budget && design_type != models.DesignType_Settlement && design_type != models.DesignType_Audit {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计类型错误",
		})
		return
	}

	data, err := c.svcDesign.LoadProjectLabor(designcode, design_type)
	if err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":       "controllers",
			"operation":   "LoadProjectLabor",
			"design_code": designcode,
		}).Errorf("加载项目人工条目信息失败: %v", err)
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "加载项目人工条目信息失败",
		})
		return
	}
	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code:  0,
		Msg:   "success",
		Count: len(data), // 总记录数
		Data:  data,
	})
}

// LoadProjectMaterial 加载项目材料条目信息
func (c *designControllerImpl) LoadProjectMaterial(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {

		return
	}

	designcode := ctx.Query("design_code")
	if designcode == "" {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计编号不能为空",
		})
		return
	}
	dtype := ctx.Query("design_type")
	design_type, err := strconv.Atoi(dtype)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计类型错误",
		})
		return
	}

	if design_type != models.DesignType_Budget && design_type != models.DesignType_Settlement && design_type != models.DesignType_Audit {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计类型错误",
		})
		return
	}
	data, err := c.svcDesign.LoadProjectMaterial(designcode, design_type)
	if err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":       "controllers",
			"operation":   "LoadProjectMaterial",
			"design_code": designcode,
		}).Errorf("加载项目材料条目信息失败: %v", err)
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "加载项目材料条目信息失败",
		})
		return
	}
	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code:  0,
		Msg:   "success",
		Count: len(data), // 总记录数
		Data:  data,
	})
}

// GetMaterialPrice 获取材料单价
func (c *designControllerImpl) GetMaterialPrice(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {
		return
	}

	type MaterialPriceRequest struct {
		DesignType    int      `json:"design_type"`
		OrgCode       string   `json:"org_code"`
		MaterialCodes []string `json:"material_codes"`
	}
	var req MaterialPriceRequest

	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "材料价格查询参数错误",
		})
		return
	}
	var result []models.MaterialPrice
	for _, code := range req.MaterialCodes {
		result = append(result, models.MaterialPrice{
			MaterialCode: code,
			Price:        0,
		})
	}

	if len(req.MaterialCodes) == 0 {
		ctx.JSON(http.StatusOK, models.ResponseModel{
			Code: 1,
			Msg:  "请提供材料编码",
		})
		return
	}

	pricecount, err := c.svcDesign.GetMaterialPrice(result, req.OrgCode, req.DesignType)
	if err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":     "controllers",
			"operation": "GetMaterialPrice",
			"error":     err.Error(),
		}).Error("获取材料单价失败")
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "获取材料单价失败",
		})
		return
	}

	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code:  0,
		Msg:   "success",
		Count: pricecount,
		Data:  result,
	})

}

func (c *designControllerImpl) GetNormConversion(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {
		return
	}

	normcode := ctx.Query("normcode")
	if normcode == "" {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "人工定额编码不能为空",
		})
		return
	}

	data, err := c.svcDesign.GetNormConversion(normcode)
	if err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":     "controllers",
			"operation": "GetNormConversion",
			"error":     err.Error(),
		}).Error("获取人工定额转换系数失败")
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "获取人工定额转换系数失败: " + normcode,
		})
		return
	}

	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code: 0,
		Msg:  "success",
		Data: data,
	})
}

// 获取项目施工成本（不含税）
func (c *designControllerImpl) GetConstructionCost(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {
		return
	}

	dcode := ctx.Query("design_code")
	if dcode == "" {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计编号不能为空",
		})
		return
	}
	design_type := ctx.Query("design_type")
	if design_type == "" {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计类型不能为空",
		})
		return
	}
	dtype, err := strconv.Atoi(design_type)
	if err != nil {
		//ctx.JSON(http.StatusBadRequest, gin.H{"信息": "项目类型错误"})
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计类型错误",
		})
		return
	}

	data, err := c.svcDesign.GetConstructionCost(dcode, dtype)
	if err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":     "controllers",
			"operation": "GetConstructionCost",
			"error":     err.Error(),
		}).Error("获取项目施工成本失败")
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "获取项目施工总成本失败: " + dcode,
		})
		return
	}
	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code: 0,
		Msg:  "success",
		Data: data,
	})
}

// LoadProjectOtherFee 加载项目其他费用条目信息
func (c *designControllerImpl) LoadProjectOtherFee(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {
		return
	}
	dcode := ctx.Query("design_code")
	if dcode == "" {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计编号不能为空",
		})
		return
	}
	design_type := ctx.Query("design_type")
	if design_type == "" {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计类型不能为空",
		})
		return
	}
	dtype, err := strconv.Atoi(design_type)
	if err != nil {
		//ctx.JSON(http.StatusBadRequest, gin.H{"信息": "项目类型错误"})
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计类型错误",
		})
		return
	}

	data, err := c.svcDesign.LoadProjectOtherFee(dcode, dtype)
	if err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":     "controllers",
			"operation": "LoadProjectOtherFee",
			"error":     err.Error(),
		}).Error("获取项目其他费用条目信息失败")
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "获取项目其他费用条目信息失败: " + dcode,
		})
		return
	}
	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code: 0,
		Msg:  "success",
		Data: data,
	})

}

// SaveProjectOtherFee 保存项目其他费用条目信息
func (c *designControllerImpl) SaveProjectOtherFee(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {
		return
	}

	type otherList struct {
		DesignCode string                `json:"design_code"`
		DesignType int                   `json:"design_type"`
		OtherFee   []models.OtherFeeItem `json:"other_table"`
	}

	var req otherList
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"信息": "参数错误"})
		return
	}

	if err := c.svcDesign.SaveProjectOtherFee(req.DesignCode, req.DesignType, req.OtherFee); err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":     "controllers",
			"operation": "SaveProjectOtherFee",
			"error":     err.Error(),
		}).Error("保存项目其他费用条目信息失败")
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "保存项目其他费用条目信息失败: " + req.DesignCode,
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{"code": 0, "msg": "success"})
}

func (c *designControllerImpl) SetProjectFiled(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {
		return
	}
	// 验证参数
	type fileReq struct {
		DesignCode   string `json:"design_code"`
		ProjectState int    `json:"project_state"`
	}

	var req fileReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "设计存档参数错误",
		})
		return
	}
	if req.DesignCode == "" {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目设计编号不能为空",
		})
		return
	}
	if req.ProjectState == 0 {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "项目状态不能为空",
		})
		return
	}
	// 调用服务层方法
	if err := c.svcDesign.SetProjectFiled(req.DesignCode, req.ProjectState); err != nil {
		logger.WithFields(map[string]interface{}{
			"layer":     "controllers",
			"operation": "FileProject",
			"error":     err.Error(),
		}).Error("项目存档失败")
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "项目存档失败: " + req.DesignCode,
		})
		return
	}
	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code: 0,
		Msg:  "success",
	})
}

func (c *designControllerImpl) DeleteProject(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {
		return
	}
	// 验证参数
	designCode := ctx.Param("design_code")
	designType, err := strconv.Atoi(ctx.Query("design_type"))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, models.ResponseModel{
			Code: 1,
			Msg:  "无法确定项目要删除的设计类型",
		})
		return
	}

	// 调用服务层方法
	if err := c.svcDesign.DeleteProject(designCode, designType); err != nil {
		ctx.JSON(http.StatusInternalServerError, models.ResponseModel{
			Code: 1,
			Msg:  "删除项目失败: " + designCode,
		})
		return
	}

	ctx.JSON(http.StatusOK, models.ResponseModel{
		Code: 0,
		Msg:  "删除项目成功",
	})
}

func (c *designControllerImpl) GetProjectBalance(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {
		return
	}
	// 验证参数
	// designCode := ctx.Param("design_code")
	// approvalCode := ctx.Query("approval_code")

}

func (c *designControllerImpl) GetProjectOutbound(ctx *gin.Context) {
	// 验证用户信息
	if !c.svcAuth.JWTAuthUser(ctx) {
		return
	}
	// 验证参数
	// designCode := ctx.Param("design_code")
	// approvalCode := ctx.Query("approval_code")

}
